<?php
/**
 * App端 购物车的处理
 * Author: lyun
 * Date: 2018/11/21
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Api\V1\App\Cart;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Cart\CartDeleteRequest;
use App\Http\Requests\Cart\CartStoreRequest;
use App\Http\Requests\Cart\CartUpdateOptionRequest;
use App\Http\Requests\Cart\CartUpdateRequest;
use App\Http\Services\Api\V1\App\Cart\ShoppingCartService;
use App\Models\Cart\Cart;
use App\Models\Customers\Customer;
use App\Models\Products\Product;
use App\Services\Utils\Guard;

class ShoppingCartController extends ApiController
{
    public $cartService;

    /**
     * ShoppingCartController constructor.
     */
    public function __construct()
    {
        $this->cartService = new ShoppingCartService();
    }

    /**
     * app端 获取当前用户的购物车信息
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function index()
    {
        /** @var Customer $customer */
        $customer = Guard::customer();

        $cart_products = $this->cartService->getShoppingCartData($customer->customer_id);

        return $this->response(compact('cart_products'));
    }

    /**
     * app端 获取当前用户的购物车商品数量
     *
     * @return array|\Illuminate\Http\Response
     */
    public function create()
    {
        /** @var Customer $customer */
        $customer = Guard::customer();

        $cart_num = $this->cartService->getCartNum($customer->customer_id);

        return $this->response(compact('cart_num'));
    }

    /**
     *  app端 添加商品到购物车
     *
     * @param CartStoreRequest $request
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function store(CartStoreRequest $request)
    {
        $cart_data = $this->assembleCartData($request);

        $stored = $this->cartService->storeCartData($cart_data);

        return $this->response(compact('stored'));
    }

    /**
     * app端 更新购物车商品的数量
     *
     * @param CartUpdateRequest $request
     * @param $cart_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(CartUpdateRequest $request, $cart_id)
    {
        $quantity = $request->quantity;
        $cart = $request->cart;

        $updated = $this->cartService->updateShoppingCartData($cart, $quantity);

        return $this->response(compact('updated'));
    }

    /**
     * app端 更新购物车商品的选项值
     *
     * @param CartUpdateOptionRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function updateCartOption(CartUpdateOptionRequest $request)
    {
        $product_id = $request->product_id;
        $option = $request->option;
        $cart = $request->cart;

        $updated = $this->cartService->updateCartOption($option, $cart, $product_id);

        return $this->response(compact('updated'));
    }

    /**
     * app端 批量删除购物车商品
     *
     * @param CartDeleteRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function delete(CartDeleteRequest $request)
    {
        /** @var Customer $customer */
        $customer = Guard::customer();

        $cart_ids = $request->cart_ids;

        $deleted = $this->cartService->deleteShoppingCartData($cart_ids, $customer->customer_id);

        return $this->response(compact('deleted'));
    }

    /**
     * @param CartStoreRequest $request
     * @return array
     */
    private function assembleCartData(CartStoreRequest $request)
    {
        return [
            'api_id'       => Cart::API_ID_DEFAULT,
            'recurring_id' => Cart::RECURRING_ID_DEFAULT,
            'customer_id'  => Guard::customer()->customer_id ?? Cart::CUSTOMER_ID_DEFAULT,
            'session_id'   => $request->session_id ?: Cart::newSessionId(),
            'product_id'   => $request->product_id,
            'option'       => $request->option ?? [],
            'quantity'     => $request->quantity,
            'date_added'   => (string) now(),
        ];
    }
}
